from utils.sql_utils import SQLManager
from models.user_model import UserModel
from utils.id_utils import IdUtils
from utils.time_utils import TimeUtils
import random
import uuid
class UserService:
    def get_user_by_user_id(user_id):
        sql = """
            SELECT 
                rau.unique_id AS user_id,
                rau.admin_user_name AS user_name,
                rau.avatar_url AS user_avatar_url
            FROM 
                rb_admin_user rau
            WHERE 
                unique_id = %s
        """
        params = (user_id,)
        data = SQLManager().get_one(sql, params)  # 获取分页数据
        return data

    def update_user_avatar(avatar_url,user_id):
        sql = """
            UPDATE 
                rb_admin_user 
            SET 
                avatar_url = %s 
            WHERE 
                unique_id = %s
        """
        params = (avatar_url,user_id)
        return SQLManager().insert(sql, params)
    
    def update_user_pass(password,user_id):
        sql = """
            UPDATE 
                rb_admin_user 
            SET 
                password = %s
            WHERE 
                unique_id = %s
        """
        params = (password,user_id)
        return SQLManager().insert(sql, params)
    

    def register_user(password,user_name,user_id):
        random_id = random.randint(1, 5)
        base_url = f"http://117.72.37.61:9000/gh-rbk/avatar/{random_id}.png"
        sql = """
            INSERT INTO 
                rb_admin_user 
                (unique_id,admin_user_name,avatar_url,phone,status,password,created_at,modify_time,is_deleted) 
            VALUES 
                (%s,%s,%s,%s,%s,%s,%s,%s,%s);
        """
        params = (user_id,user_name,base_url,'',1,password,TimeUtils.get_current_time(),'',0)
        return SQLManager().insert(sql, params)
    
    def query_user_by_user_name(user_name):
        sql = """
            SELECT 
                rau.unique_id AS user_id,
                rau.admin_user_name AS user_name,
                rau.avatar_url AS user_avatar_url
            FROM 
                rb_admin_user rau
            WHERE 
                rau.admin_user_name = %s
        """
        params = (user_name,)
        data = SQLManager().get_one(sql, params)  # 获取分页数据
        return data